package com.qfedu.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.ResponseData;
import com.qfedu.entity.Skill;
import com.qfedu.entity.User;
import com.qfedu.service.SkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/skill")
public class SkillController {
    @Autowired
    private SkillService skillService;

    @RequestMapping("/querySkill.do")
    public ResponseData querySkill(int page,int limit) {
        PageHelper.startPage(page,limit);

        List<Skill> allSkill = skillService.findAllSkill();

        PageInfo<Skill> pageInfo = new PageInfo<>(allSkill);
        int total = (int) pageInfo.getTotal();

        ResponseData<Object> responseData = new ResponseData<>();

        responseData.setCode(0);
        responseData.setCount(total);
        responseData.setMsg("技术相关信息");
        responseData.setData(allSkill);

        return responseData;
    }

    @RequestMapping("/add.do")
    public ResponseData addSkill(Skill skill, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        String no = user.getNo();

        skill.setU_no(no);
        skill.setCreateTime(new Date());

        int i = skillService.addSkill(skill);

        ResponseData<Object> responseData = new ResponseData<>();
        if (i > 0) {
            responseData.setCode(0);
            responseData.setMsg("申请成功");
        } else {
            responseData.setCode(1);
            responseData.setMsg("申请失败");
        }

        return responseData;
    }
}
